﻿<template>
  <a-card :bordered="false">
    <a-modal
      :confirmLoading="loading"
      :visible="visible"
      title="订单于返料规则"
      width="30%"
      @cancel="
        () => {
          this.visible = false
        }
      "
    >
      <template slot="footer">
        <a-button key="back" @click="visible = false">
          关闭
        </a-button>
      </template>
      <div class="flax">
        <div class="download">
          <a-button style="margin-left: 8px" @click="openhtml()">
            <a-icon type="download"/>
            导出订单返料规则
          </a-button>
        </div>
        <a-upload
          :action="$rootUrl + '/OrderManagement/SO_SOMain/ImportForUpdateSO_SOMainAndRule'"
          :headers="headers"
          :multiple="true"
          name="file"
          @change="handleChange"
        >
          <a-button style="margin-left: 8px">
            <a-icon type="upload"/>
            导入订单返料规则
          </a-button>
        </a-upload>
      </div>
    </a-modal>
    <div class="table-operator">
      <a-button v-if="hasPerm('SO_SOMain.Add')" icon="plus" type="primary" @click="handleAdd()">新建</a-button>
      <a-button
        v-if="hasPerm('SO_SOMain.Delete')"
        :disabled="!hasSelected()"
        :loading="loading"
        icon="minus"
        type="primary"
        @click="handleDelete(selectedRowKeys)"
      >删除
      </a-button
      >
      <a-button icon="redo" type="primary" @click="getDataList()">刷新</a-button>
      <a-button
        v-if="hasPerm('Cust_CustomerLinkMan.Import')"
        style="margin-left: 8px"
        type="primary"
        @click="
          () => {
            this.visible = true
          }
        "
      >返料规则绑定
      </a-button>
    </div>

    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="4" :sm="24">
            <a-form-item label="查询类别">
              <a-select v-model="queryParam.condition" allowClear>
                <a-select-option key="cSOCode">销售订单号</a-select-option>
                <a-select-option key="cCusName">客户名称</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="24">
            <a-form-item>
              <a-input v-model="queryParam.keyword" placeholder="关键字" @keydown.enter="getDataList"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button
              type="primary"
              @click="
                () => {
                  this.pagination.current = 1
                  this.getDataList()
                }
              "
            >查询
            </a-button
            >
            <a-button style="margin-left: 8px" @click="repossess">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      :bordered="true"
      :columns="columns"
      :dataSource="data"
      :loading="loading"
      :pagination="pagination"
      :rowKey="(row, index) => row.ID"
      :scroll="{ y: 530 }"
      size="small"
      @change="handleTableChange"
    >
      <span slot="CreateTime" slot-scope="CreateTime">{{ timeFormat(CreateTime) }}</span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="handleEdit(record)">查看</a>
          <a-divider v-if="hasPerm('SO_SOMain.Edit') && hasPerm('SO_SOMain.Delete')" type="vertical"/>
          <a v-if="hasPerm('SO_SOMain.Delete')" @click="handleDelete([record.Id])">删除</a>
        </template>
      </span>
    </a-table>

    <edit-form ref="editForm" :parentObj="this"></edit-form>
  </a-card>
</template>

<script>
import Token from '@/utils/cache/TokenCache'
import EditForm from './EditForm'
import moment from 'moment'
import { download } from '@/components/download'

const columns = [
  { title: '销售订单号', dataIndex: 'cSOCode', width: '15%' },
  {
    title: '单据日期',
    dataIndex: 'dDate',
    align: 'center',
    customRender: (value, row) => {
      return row.dDate.substring(0, 10)
    },
    width: '10%'
  },
  { title: '客户编码', dataIndex: 'cCusCode', width: '10%' },
  { title: '客户名称', dataIndex: 'cCusName', width: '25%' },
  // { title: '销售类型', dataIndex: 'cSTCode' },
  { title: '销售类型', dataIndex: 'cBusType', align: 'center', width: '10%' },
  // { title: '组织机构编码', dataIndex: 'cDepCode', width: '10%' },
  // { title: '销售经理', dataIndex: 'RealName', width: '10%' },
  { title: '收货地址', dataIndex: 'cCusOAddress', width: '25%', ellipsis: true },
  { title: '返料规则', dataIndex: 'RuleName', width: '15%' },
  { title: '操作', dataIndex: 'action', align: 'left', scopedSlots: { customRender: 'action' }, width: '80px' }
]

export default {
  components: {
    EditForm,
    Token
  },
  mounted () {
    this.getDataList()
  },
  data () {
    return {
      headers: {
        authorization: 'Bearer ' + Token.getToken()
      },
      data: [],
      visible: false,
      pagination: {
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['25', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
        showSizeChanger: true
      },
      filters: {},
      sorter: { field: 'dDate', order: 'desc' },
      loading: false,
      columns,
      queryParam: { condition: 'cCusName' },
      selectedRowKeys: []
    }
  },
  methods: {
    openhtml () {
      this.$http.post('/OrderManagement/SO_SOMain/ExportSO_SOMainAndRuleToExcel', {}).then(resJson => {
        if (resJson.Success) {
          download(resJson.Data.fileUrl, resJson.Data.fileName)
          this.visible = false
        } else {
          this.$message.error(resJson.Msg)
        }
      })
    },
    repossess () {
      this.queryParam = { condition: 'cCusName' }
    },
    handleTableChange (pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }

      this.getDataList()
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    getDataList () {
      this.selectedRowKeys = []

      this.loading = true
      this.$http
        .post('/OrderManagement/SO_SOMain/GetDataListByMySelfCustomer', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
    // 导入文件类型
    handleChange (info) {
      if (info.file.status !== 'uploading') {
      }
      if (info.file.status === 'done') {
        if (info.file.response.Success === false) {
          this.$message.error(`${info.file.name}文件,${info.file.response.Msg},请检查文件内容！ `)
        } else {
          this.$message.success(`${info.file.name}  文件上传成功！`)
          this.getDataList()
        }
        this.visible = false
      } else if (info.file.status === 'error') {
        this.$message.error(`${info.file.name} 文件上传失败！`)
      }
    },
    hasSelected () {
      return this.selectedRowKeys.length > 0
    },
    handleAdd () {
      this.$refs.editForm.openForm(null, '新建销售订单主表信息')
    },
    handleEdit (id) {
      this.$refs.editForm.openForm(id, '订单详情')
    },
    handleDelete (ids) {
      var thisObj = this
      this.$confirm({
        title: '确认删除吗?',
        onOk () {
          return new Promise((resolve, reject) => {
            thisObj.$http.post('/OrderManagement/SO_SOMain/DeleteData', ids).then(resJson => {
              resolve()

              if (resJson.Success) {
                thisObj.$message.success('操作成功!')

                thisObj.getDataList()
              } else {
                thisObj.$message.error(resJson.Msg)
              }
            })
          })
        }
      })
    },
    timeFormat (val) {
      // 时间格式化函数
      return moment(val).format('YYYY-MM-DD HH:mm')
    }
  }
}
</script>

<style scoped>
.flax {
  display: flex;
  justify-content: space-between;
}

/* .download {
  float: right;
  display: flex;
  align-items: center;
} */
</style>
